package com.kye.express.business.order_list.subfragment.viewbinder;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.base.Logger;
import com.kye.express.R;
import com.kye.express.databinding.LayoutOrderListItemBinding;
import com.kye.model.bean.order.OrderItemBean;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 29/12/2017
 * @updated : 29/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class OrderListItemViewBinder extends ItemViewBinder<OrderItemBean, OrderListItemViewBinder.ViewHolder> {

    private OnItemListener onItemListener;

    public OrderListItemViewBinder(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.layout_order_list_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull OrderItemBean item) {
        holder.bind(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private OrderItemBean item;
        private LayoutOrderListItemBinding binding;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.container)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onItemListener != null) {
                                onItemListener.onItemOrderClicked(item);
                            }
                        }
                    });
            RxView.clicks(binding.orderGoodsStatus)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {

                        }
                    });
            RxView.clicks(binding.orderGradeDone)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {

                        }
                    });
            RxView.clicks(binding.orderGradeUndone)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (onItemListener != null) {
                                onItemListener.onItemOrderScoreClicked(item);
                            }
                        }
                    });
            RxView.clicks(binding.orderGradeCannot)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {

                        }
                    });
            RxView.clicks(binding.orderGradeNotPermitteds)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {

                        }
                    });
            RxView.clicks(binding.orderGradeOverdue)
                    .compose(new DebounceObservableTransformer<Object>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {

                        }
                    });
        }

        public void bind(final OrderItemBean item) {
            this.item = item;
            binding.setData(item);
            binding.orderGoodsStatus.setVisibility(View.GONE);
            binding.orderGradeDone.setVisibility(View.GONE);
            binding.orderGradeUndone.setVisibility(View.GONE);
            binding.orderGradeCannot.setVisibility(View.GONE);
            binding.orderGradeNotPermitteds.setVisibility(View.GONE);
            binding.orderGradeOverdue.setVisibility(View.GONE);
            String orderState = item.getOrderState();
            switch (orderState) {
                case "取货中":
                case "订单处理中":
                    updateGoodsToTakeData(item);
                    break;
                case "取货完毕":
                    updateGoodsTakenData(item);
                    break;
                default:
                    throw new IllegalStateException("orderState : " + orderState);
            }
        }

        public void updateGoodsToTakeData(OrderItemBean item) {
            binding.orderGoodsStatus.setVisibility(View.VISIBLE);
            switch (item.getGoodsState()) {
                case "0":
                    binding.orderGoodsStatus.setText("处理中");
                    break;
                case "1":
                    binding.orderGoodsStatus.setText("已调度");
                    break;
                case "2":
                    binding.orderGoodsStatus.setText("已到达");
                    break;
                default:
                    binding.orderGoodsStatus.setText("处理中");
                    Logger.d("goods state error : " + item.getGoodsState());
//                    throw new IllegalArgumentException("goods state error");

            }
        }

        public void updateGoodsTakenData(OrderItemBean item) {
//            评分状态(可评分（code=1）)(已评分（code=2）)(已过期（code=3）)(没权限（code=4）)(不能评（code=5）)
            String scoreCode = item.getScoreCode();
            switch (scoreCode) {
                case "1":
                    binding.orderGradeUndone.setVisibility(View.VISIBLE);
                    break;
                case "2":
                    binding.orderGradeDone.setVisibility(View.VISIBLE);
                    break;
                case "3":
                    binding.orderGradeOverdue.setVisibility(View.VISIBLE);
                    break;
                case "4":
                    binding.orderGradeNotPermitteds.setVisibility(View.VISIBLE);
                    break;
                case "5":
                    binding.orderGradeCannot.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }
        }
    }

    public interface OnItemListener {
        void onItemOrderClicked(OrderItemBean item);

        void onItemOrderScoreClicked(OrderItemBean item);
    }
}
